import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { DynamicDialogRef, DynamicDialogConfig } from 'primeng/api';
import { ConfirmationService, SelectItem } from 'primeng/primeng';
import { FreeflowService } from '../freeflow.service';
import { MdlDialogService } from '@angular-mdl/core';
import { ProcDef } from '../freeflow.model';

@Component({
    templateUrl: './ff-addbizproc.component.html',
    styleUrls: ['./ff-addbizproc.component.scss'],
    providers: [FreeflowService]
})
export class FfAddBizProcComponent implements OnInit {

    bizProcForm: FormGroup;
    bizs: SelectItem[] = [];
    biz: SelectItem;
    procDef: ProcDef = new ProcDef({});

    constructor(
        private fb: FormBuilder,
        public ref: DynamicDialogRef,
        public config: DynamicDialogConfig,
        private dialogService: MdlDialogService,
        private FreeflowService: FreeflowService) {
    }

    ngOnInit() {
        /**初始化业务类型流程选项 */
        this.FreeflowService.listBizs().subscribe(data => {
            data.forEach(element => {
                this.bizs.push({ label: element['bizName'], value: element['bizId'] });
            });
        });
        this.buildForm();
    }
    /**新增 */
    save() {
        let formDef = this.bizProcForm.value;
        if (this.biz) {
            formDef["bizId"] = this.biz.value;
            formDef["bizName"] = this.biz.label;
        }
        this.FreeflowService.saveUpdateProc(formDef).subscribe(data => {
            this.ref.close("porc_" + data);
        });
    }

    /**取消关闭窗口 */
    cancel() {
        this.ref.close();
    }

    buildForm(): void {
        this.bizProcForm = this.fb.group({
            'bizName': [this.procDef.bizName],
            'procDefName': [this.procDef.procDefName]
        });
        this.bizProcForm.valueChanges
            .subscribe(data => this.onValueChanged(data));
        this.onValueChanged();
    }

    onValueChanged(data?: any) {
        if (!this.bizProcForm) { return; }
        const form = this.bizProcForm;
        for (const field in this.formErrors) {
            this.formErrors[field] = '';
            const control = form.get(field);
            if (control && control.dirty && !control.valid) {
                const messages = this.validationMessages[field];
                for (const key in control.errors) {
                    this.formErrors[field] += messages[key] + ' ';
                }
            }
        }
    }

    formErrors = {
        'formName': ''
    };

    validationMessages = {
        'formName': {
            'required': '该字段为必填项',
            'minlength': '长度必须大于2',
            'maxlength': '长度必须小于20'
        }
    };

    /**业务类型变更 */
    bizChange($event: any) {
        this.biz = null;
        this.bizs.forEach(data => {
            if (data.label == $event.value) {
                this.biz = data;
            }
        });
    }


}
